package com.hengpeng.itfints.config;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.hengpeng.common.config.ConfigManager;
import com.hengpeng.common.persist.QueryParameter;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.itfinbase.persist.PageConfig;
import com.hengpeng.itfints.config.vo.Common;
import com.hengpeng.itfints.port.PortFactory;

/**
 * <pre>
 * TODO 开发测试先采用读取本地配置方式,项目上线后可把配置存到数据库中
 * </pre>
 * @author chenjian
 * @version 1.0, 2012-5-26
 */
public class PageConfigLoadImpl implements PageConfigLoad
{
	public static final Log log = LogFactory.getLog(PageConfigLoadImpl.class);

	@Autowired
	@Qualifier("portFactoryImpl")
	private PortFactory portFactory;

	/**
	 * java属性文件读取
	 */
	private Set<String> propertiesKey;

	/**
	 * 通用配置
	 */
	private String commonKey;

	public void init()
	{
		log.info("系统启动,开始加载配置文件...");
		if (propertiesKey != null)
		{
			for (String key : propertiesKey)
			{
				loadProperties(key);
			}
		}
		if (commonKey != null && !commonKey.equals(""))
		{
			loadCommon(commonKey);
		}
	}

	@SuppressWarnings("all")
	private PageConfig getPageConfig(String key)
	{
		QueryParameter<PageConfig> query = new QueryParameter<PageConfig>();
		DynamicQuery customQuery = new DynamicQuery(PageConfig.class);
		customQuery.eq("id", key);
		query.setCustomQuery(customQuery);
		List<PageConfig> result = portFactory.remoteQuery(query).getResults();
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	public void reload(String key)
	{
		if (propertiesKey != null && propertiesKey.contains(key))
		{
			loadProperties(key);
		}
		else if (commonKey != null && commonKey.equals(key))
		{
			loadCommon(key);
		}
	}

	public void loadProperties(String key)
	{
		String decodeUrl = null;
		try
		{
			decodeUrl = java.net.URLDecoder.decode(this.getClass().getResource("/").getPath(), "utf-8") + "config/"
					+ key + ".properties";
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		ConfigManager.setPropertiesPathConfig(key, decodeUrl, true);
	}

	public void loadCommon(String key)
	{
		String decodeUrl = null;
		try
		{
			decodeUrl = java.net.URLDecoder.decode(this.getClass().getResource("/").getPath(), "utf-8") + "config/"
					+ key + ".xml";
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		ConfigManager.setXmlPathConfig(key, decodeUrl, true, null, Common.class);
	}

	public Set<String> getPropertiesKey()
	{
		return propertiesKey;
	}

	public void setPropertiesKey(Set<String> propertiesKey)
	{
		this.propertiesKey = propertiesKey;
	}

	public String getCommonKey()
	{
		return commonKey;
	}

	public void setCommonKey(String commonKey)
	{
		this.commonKey = commonKey;
	}

}
